package com.blb.zycommunity.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.zycommunity.entity.*;
import com.blb.zycommunity.service.IZyRoomService;
import com.blb.zycommunity.service.impl.ZyBuildingServiceImpl;
import com.blb.zycommunity.service.impl.ZyCommunityServiceImpl;
import com.blb.zycommunity.service.impl.ZyRoomServiceImpl;
import com.blb.zycommunity.service.impl.ZyUnitServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
/**
 * <p>
 * 房间  前端控制器
 * </p>
 *
 * @author chenheng
 * @since 2023-11-20
 */
@CrossOrigin
@RestController
public class ZyRoomController {

    @Resource
    private ZyRoomServiceImpl zyRoomService;

    @Autowired
    private IZyRoomService iZyRoomService;

    @Resource
    private ZyCommunityServiceImpl zyCommunityService;

    @Resource
    private ZyBuildingServiceImpl zyBuildingService;

    @Resource
    private ZyUnitServiceImpl zyUnitService;


    //房屋信息集合模糊查询
    @GetMapping("/zyRoom-list")
    public ResponseResult<Page<ZyRoom>> selectByAll(@RequestParam(required = false,defaultValue = "1")Long current,
                                                    @RequestParam(required = false,defaultValue = "6")Long size,
                                                    @RequestParam(required = false,defaultValue = "")String communityId,
                                                    @RequestParam(required = false,defaultValue = "")String unitId,
                                                    @RequestParam(required = false,defaultValue = "")String buildingId,
                                                    @RequestParam(required = false,defaultValue = "")String roomStatus
    ){
        IPage<ZyRoom> zyRoomIPage = zyRoomService.selectByAll(current,size,unitId,buildingId,roomStatus,communityId);
        return ResponseResult.ok(zyRoomIPage);
    }
    //加载楼栋和单元
    @GetMapping("/zyRoom-buildingsAndUnits")
    public ResponseResult<List<ZyBuilding>> findBuildingAndUnit() {
        List<ZyBuilding> buildingAndUnit = zyBuildingService.getBuildingAndUnit();
        return ResponseResult.ok(buildingAndUnit);
    }

    //楼栋列表
    @GetMapping("/zyBuilding-room")
    public ResponseResult find(){
        List<ZyBuilding> zyBuildings = zyBuildingService.list();
        return ResponseResult.ok(zyBuildings);
    }
    //单元列表
    @GetMapping("/zyUnit-room")
    public ResponseResult findA(){
        List<ZyUnit> zyUnits = zyUnitService.list();
        return ResponseResult.ok(zyUnits);
    }
    //小区列表
    @GetMapping("/zyCommunity-room")
    public ResponseResult findAl(){
        List<ZyCommunity> list = zyCommunityService.list();
        return ResponseResult.ok(list);
    }

    //房屋添加
    @PostMapping("/zyRoom-add")
    public ResponseResult addUser(@RequestBody ZyRoom zyRoom){
        zyRoom.setCreateTime(LocalDateTime.now());
        zyRoomService.save(zyRoom);
        return ResponseResult.ok("ok");
    }

    //房屋修改
    @PutMapping("/zyRoom-update")
    public ResponseResult updateZyUnit(@RequestBody ZyRoom zyRoom){
        zyRoomService.updateById(zyRoom);
        return ResponseResult.ok("ok");
    }



    //批量删除
    @DeleteMapping("/deleteRoomIds/{roomIds}")
    public ResponseResult deleteByIds(@PathVariable Long[] roomIds){
        for (Long roomId : roomIds) {
            System.out.println(roomId);
        }
        for (Long roomId : roomIds) {
            zyRoomService.removeById(roomId);
        }
        return ResponseResult.ok("ok");
    }

}
